/**
 * 出货信息维护 - 编辑页面
 * @author mengyb
 * @date 2019-05-04
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui=win.layui,
        table,
        vueData = {
            isAdd: false,
            isDetail:false,
            form: '',
            mx:'',
            rules: {
                GWEC002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEC005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEC012: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEC011: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWECDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                // NAME_AZP: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // AZP02: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ]
            },
            selectOptions:{
            }
        },
        cols=[[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            ,{field:'ROW_ID',width:60,title:'序号'}
            , {field: 'GWEC014', width: 80, title: '项目编号'}
            , {field: 'GWEC013', width: 80, title: '设备编号'}
            , {field: 'GWEC003', width: 180, title: '销售订单号'}
            , {field: 'GWEC101', width: 80, title: '订单项次'}
            , {field: 'GWECDOCDATE', width: 80, title: '订单日期'}
            , {field: 'GWEC004', width: 100, title: '产品编号'}
            , {field: 'GWEC102', width: 80, title: '外文名称',hide:true}
            , {field: 'GWCS002', width: 80, title: '销售单位'}
            , {field: 'GWCS003', width: 80, title: '销售单数量'}
            , {field: 'COUNT', width: 80, title: '已出货数量'}
            , {field: 'GWEC009', width: 100, title: '本次出货数量',edit:'text'}
            , {field: 'GWEC002', width: 80, title: '出货日期'}
            , {field: 'NAME_USER', width: 80, title: '人员'}
            , {field: 'NAME_ORG', width: 80, title: '归属组织'}
            , {field: 'GWECDOCNO', width: 150, title: '出货单单号'}
            , {field: 'GWECSEQ', width: 80, title: '项次'}
        ]],
        callObj,
        moduleName,
        layOpenIndex,
        listData=[],
        gwecTable
    ;

    var Gwei005Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei005_edit',
                _loadTplsFull:[
                    {dom:'gwei005_edit_add',tpl:'/gwei005_edit_add'},
                    {dom:'common_open_window',tpl:'common/_open_window'}
                ]
            }, win);
        },

        //初始化表格
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwecTable = table.render({
                    elem: app.getByid(doc, 'gwei005_edit_table'),
                    id: 'gwei005_edit_table',
                    cols: cols,
                    data: listData,
                    loading: true,
                    page: true,
                    height: 300,
                    where:'',
                });


                //监听单元格编辑
                table.on('edit(gwei005_edit_table)', function (obj) {
                    if (obj.field == 'GWEC009') {
                        if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                            obj.updateOfEdit({
                                'GWEC009': ''
                            });
                            app.showMessage('出货数量必需为数字');
                            return;
                        } else {
                            if(obj.value<0){
                                obj.updateOfEdit({
                                    'GWEC009': ''
                                });
                                app.showMessage('本次出货数量不可小于0');
                                return;
                            }else if(obj.value>obj.data.GWCS003-obj.data.COUNT){
                                obj.updateOfEdit({
                                    'GWEC009': ''
                                });
                                app.showMessage('本次出货数量大于允许出货数量');
                                return;
                            }
                            obj.updateOfEdit({
                                'GWEC009': obj.value
                            });
                        }
                    }
                });

            });
        },

        //表格重载
        tableReload: function (listData) {
            // table.reload('gwei005_edit_table', {
            //     data: listData
            // });
            self._reloadTable({id: 'gwei005_edit_table', data: listData});
        },



        //保存import数据
        saveImportData:function(rowdata){
            var tableList=gwecTable.getDatas();
            var length = tableList.length;
            var num = 0;
            if (length != 0) {
                var obj = tableList.find(function (value, index) {
                    return index==length-1;
                });
                num=obj.GWECSEQ;
            }
            for (var i = 0; i < rowdata.length; i++) {
                num = num + 1;
                tableList.push({
                    "ROW_ID": num,
                    "GWEC014": rowdata[i]["GWCS046"],
                    "GWEC013": rowdata[i]["GWCS042"],
                    "GWEC003": rowdata[i]["GWCSDOCNO"],
                    "GWEC101": rowdata[i]["GWCSSEQ"],
                    "GWECDOCDATE": rowdata[i]["GWCSDOCDATE"],
                    "GWEC004": rowdata[i]["GWCS001"],
                    "GWCS002": rowdata[i]["GWCS002"],
                    "GWCS003": rowdata[i]["GWCS003"],
                    "count": rowdata[i]["COUNT"],
                    "GWEC009": rowdata[i]["GWCS003"]-rowdata[i]["COUNT"],//订单数量-已出货数量=本次出货数量，可修改
                    "GWEC002": vueData.form.GWECDOCDATE,
                    "GWEC011": vueData.form.GWEC011,
                    "GWEC012": vueData.form.GWEC012,
                    "NAME_USER": vueData.form.NAME_USER,
                    "NAME_ORG": vueData.form.NAME_ORG,
                    "GWECDOCNO": vueData.form.GWECDOCNO,
                    "GWECSEQ": num
                });
            }
            listData=tableList;
            self.tableReload(listData);
        },

        //新增
        add: function (listData) {
            app.ajax({
                xurl: 'crm/gwei005/add',
                xdata: listData,
                loading: true,
                toJsonStr: true,
                okmsg: '出货信息新增成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule("gwei005").reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            })
        },

        //修改
        edit: function (listData) {
            app.ajax({
                xurl: 'crm/gwei005/edit',
                xdata: listData,
                loading: true,
                toJsonStr: true,
                okmsg: '出货信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwei005').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }

                }
            });
        },

        _call: function (tagData) {
            moduleName = tagData.moduleName;
            layOpenIndex=tagData.layOpenIndex;
            if (tagData.isAdd) {
                vueData.isAdd = true;
                vueData.isDetail=false;
                app.ajax({
                    type:'POST',
                    dataType:'json',
                    xurl:'crm/gwei005/getAddDef',
                    cb: function (result) {
                        app.extendUpLow({
                            target: vueData.form,
                            res: {
                                GWECDOCNO: result.data.GWECDOCNO,
                                GWECDOCDATE: result.data.GWECDOCDATE,
                                GWEC012: result.data.GWEC012,
                                NAME_ORG: result.data.NAME_ORG,
                                GWEC011: result.data.GWEC011,
                                NAME_USER: result.data.NAME_USER,
                                GWEC002:result.data.GWECDOCDATE,
                            },
                            reset: true
                        });
                        app.ajax({
                            type:'POST',
                            dataType:'json',
                            xurl:'crm/gwei005/getTreeData',
                            xdata:{
                                PARENT_ID:vueData.form.GWEC012
                            },
                            async: false,
                            cb: function (result) {
                                if(result.data.data.length>0){
                                    vueData.form.NAME_AZP = result.data.data[0].NAME;
                                    vueData.form.GWECMLEGAL = result.data.data[0].ID;
                                    vueData.form.GWECPLANT = result.data.data[0].BIZ_CODE;
                                    vueData.form.AZP02 = result.data.data[0].AZP02;
                                }

                            }
                        });

                    }
                });

                listData=[];
                self.tableReload(listData);
            } else {
                vueData.isAdd = false;
                if (tagData.isDetail == true) {//查看
                    vueData.isDetail = true;//禁用按钮
                } else {//修改
                    vueData.isDetail = false;//开启按钮
                }
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                app.ajax({
                    xurl: 'crm/gwei005/getData',
                    xdata: {
                        GWECDOCNO: callObj.data.GWECDOCNO
                    },
                    type:'POST',
                    dataType:'json',
                    cb: function (result) {
                        // vueData.form.GWECDOCDATE = result.data.GWECDOCDATE;
                        // vueData.form.GWECDOCNO = result.data.GWECDOCNO;
                        // vueData.form.GWEC002 = result.data.GWEC002;
                        // vueData.form.GWEC003 = result.data.GWEC003;
                        // vueData.form.GWEC005 = result.data.GWEC005;
                        // vueData.form.GWEC011 = result.data.GWEC011;
                        // vueData.form.GWEC012 = result.data.GWEC012;
                        // vueData.form.GWEC015 = result.data.GWEC015;
                        // vueData.form.GWAKL004 = result.data.GWAKL004;
                        // vueData.form.NAME_USER = result.data.NAME_USER;
                        vueData.form.GWECMLEGAL = result.data.GWECMLEGAL;
                        vueData.form.NAME_AZP = result.data.NAME_AZP;
                        vueData.form.GWECPLANT = result.data.GWECPLANT;
                        vueData.form.AZP02 = result.data.AZP02;
                        // vueData.form.NAME_ORG = result.data.NAME_ORG;
                        // vueData.form.GWEC107 = result.data.GWEC107;
                        // vueData.form.GWEC106 = result.data.GWEC106;
                        // vueData.form.GWEC108 = result.data.GWEC108;
                    }
                });
                app.ajax({
                    xurl: 'crm/gwei005/getGwecData',
                    xdata: {
                        GWECDOCNO: callObj.data.GWECDOCNO
                    },
                    type:'POST',
                    dataType:'json',
                    cb: function (result) {
                        listData=result.data;
                        self.tableReload(listData);
                    }
                });
            }

        },

        _ready: function () {
            table=layui.table;
            vueData.form = app.fields_(['crm_gwei005']);
            var vue = new Vue({
                el: '#gwei005_edit',
                data: vueData,
                methods: {
                    cancle: function () {
                        self._closeTpl();
                    },
                    save: function () {
                        var length=gwecTable.getDatas().length;
                        if(length==0){
                            app.showMessage("表格内容不能为空！")
                            return;
                        }
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                var list=gwecTable.getDatas();
                                var mix=[];
                                var c = 0;//统计数据中本次出货数量为空字符的个数
                                for (var i = 0; i <= list.length - 1; i++) {
                                    if (list[i]["GWEC009"] == '') {
                                        c++;
                                    }
                                }
                                if(c==0){//本次出货数量均有值
                                    for (var j = 0; j <= list.length - 1; j++) {
                                        list[j].GWECSEQ=j+1;
                                    }
                                    list.addMap(vueData.form,false);
                                    if (vueData.isAdd) {
                                        self.add(list);
                                    } else {
                                        self.edit(list);
                                    }
                                }else {
                                    app.showMessage('请先输入本次出货数量');
                                    return;
                                }

                            }
                        });
                    },

                    //客户开窗
                    openGwak:function(){
                        var tableList=gwecTable.getDatas();
                        if(tableList.length!=0){
                            app.confirm({
                                msg:'修改客户将清空表格信息，是否确认修改？',
                                ok: function () {
                                    listData=[];
                                    self.tableReload(listData);
                                    self._openTplFull({
                                        tag: 'common_open_window',
                                        tagData: {
                                            config: {
                                                method:'cq_gwak001_1',
                                                columnList: [
                                                    {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                    {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                                ],
                                            },
                                            cb: function (data) {
                                                vueData.form.GWEC005 = data.GWAK001;
                                                vueData.form.GWAKL004=data.GWAKL004;
                                                if(vueData.form.GWECPLANT != '' && vueData.form.GWECPLANT != null){
                                                    app.ajax({
                                                        xurl:'crm/gwei005/getInfo',
                                                        xdata:{
                                                            GWEC005:vueData.form.GWEC005,
                                                            GWECPLANT:vueData.form.GWECPLANT
                                                        },
                                                        type:'POST',
                                                        dataType:'json',
                                                        async:false,
                                                        cb: function (result) {
                                                            vueData.form.GWEC106 = result.data.data.GWEC106;
                                                            vueData.form.GWEC107 = result.data.data.GWEC107;
                                                            vueData.form.GWEC108 = result.data.data.GWEC108;
                                                        }
                                                    })
                                                }
                                            }
                                        },
                                        index: 1,
                                        openOpts: {
                                            title: '选择客户',
                                            area: ['600px', '465px']
                                        }
                                    });

                                }
                            })
                        }else{
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'cq_gwak001_1',
                                        columnList: [
                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                        ],
                                        // url: 'qry/getDataList',
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEC005 = data.GWAK001;
                                        vueData.form.GWAKL004=data.GWAKL004;
                                        if(vueData.form.GWECPLANT != '' && vueData.form.GWECPLANT != null){
                                            app.ajax({
                                                xurl:'crm/gwei005/getInfo',
                                                xdata:{
                                                    GWEC005:vueData.form.GWEC005,
                                                    GWECPLANT:vueData.form.GWECPLANT
                                                },
                                                type:'POST',
                                                dataType:'json',
                                                async:false,
                                                cb: function (result) {
                                                    vueData.form.GWEC106 = result.data.GWEC106;
                                                    vueData.form.GWEC107 = result.data.GWEC107;
                                                    vueData.form.GWEC108 = result.data.GWEC108;
                                                }
                                            })
                                        }
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择客户',
                                    area: ['600px', '465px']
                                }
                            });
                        }
                    },
                    //归属组织开窗
                    openOrg:function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysorg_id',
                                    columnList: [
                                        {label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '组织名称', prop: 'NAME', width: '150'}
                                    ],
                                    // url: 'qry/getDataList',
                                },
                                cb: function (data) {
                                    var newForm={
                                        GWEC011 : '',
                                        NAME_USER : '',
                                        NAME_ORG : data.NAME,
                                        GWEC012 : data.ID,
                                    };
                                    var oldForm = JSON.parse(JSON.stringify(vueData.form))
                                    Object.assign(oldForm,newForm);
                                    vueData.form=oldForm;
                                }
                            },
                            index: 1,
                            openOpts: {
                                title: '选择归属组织',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    //销售人员开窗
                    openUser:function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysuser_id',
                                    columnList: [
                                        {label: '业务员编号', prop: 'ID', width: '150'},
                                        {label: '业务员账号', prop: 'ACCOUNT', width: '150'},
                                        {label: '业务员名称', prop: 'NAME', width: '150'}
                                    ],
                                    // url: 'qry/getDataList',
                                    searchParam: {
                                        ORG_ID:vueData.form.GWEC012
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWEC011 = data.ID;
                                    vueData.form.NAME_USER = data.NAME;
                                }
                            },
                            index: 1,
                            openOpts: {
                                title: '选择业务员',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    //归属法人开窗
                    openAzp:function(){
                        var tableList=gwecTable.getDatas();
                        if(tableList.length!=0){
                            app.confirm({
                                msg:'修改归属法人将清空表格信息，是否确认修改？',
                                ok: function () {
                                    listData=[];
                                    self.tableReload(listData);
                                    self._openTplFull({
                                        tag: 'common_open_window',
                                        tagData: {
                                            config: {
                                                method:'cq_sysorg_biz_1',
                                                columnList: [
                                                    {label: 'ID', prop: 'ID', width: '150'},
                                                    {label: '归属法人', prop: 'NAME', width: '150'},
                                                    {label: '营运中心编号', prop: 'AZP01', width: '150',sortable:false,unshow:true},
                                                    {label: '营运中心名称', prop: 'AZP02', width: '150',sortable:false,unshow:true}
                                                ],
                                            },
                                            cb: function (data) {
                                                vueData.form.GWECMLEGAL = data.ID;
                                                vueData.form.NAME_AZP = data.NAME;
                                                vueData.form.GWECPLANT = data.AZP01;
                                                vueData.form.AZP02 = data.AZP02;
                                                if(vueData.form.GWEC005 != '' && vueData.form.GWEC005 != null){
                                                    app.ajax({
                                                        xurl:'crm/gwei005/getInfo',
                                                        xdata:{
                                                            GWEC005:vueData.form.GWEC005,
                                                            GWECPLANT:vueData.form.GWECPLANT
                                                        },
                                                        type:'POST',
                                                        dataType:'json',
                                                        async:false,
                                                        cb: function (result) {
                                                            vueData.form.GWEC106 = result.data.data.GWEC106;
                                                            vueData.form.GWEC107 = result.data.data.GWEC107;
                                                            vueData.form.GWEC108 = result.data.data.GWEC108;
                                                        }
                                                    })
                                                }
                                            }
                                        },
                                        index: 1,
                                        openOpts: {
                                            title: '选择归属法人',
                                            area: ['600px', '430px']
                                        }
                                    })

                                }
                            })
                        }else{
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'cq_sysorg_biz_1',
                                        columnList: [
                                            {label: 'ID', prop: 'ID', width: '150'},
                                            {label: '归属法人', prop: 'NAME', width: '150'},
                                            {label: '营运中心编号', prop: 'AZP01', width: '150',sortable:false,unshow:true},
                                            {label: '营运中心名称', prop: 'AZP02', width: '150',sortable:false,unshow:true}
                                        ],
                                    },
                                    cb: function (data) {
                                        vueData.form.GWECMLEGAL = data.ID;
                                        vueData.form.NAME_AZP = data.NAME;
                                        vueData.form.GWECPLANT = data.AZP01;
                                        vueData.form.AZP02 = data.AZP02;
                                        if(vueData.form.GWEC005 != '' && vueData.form.GWEC005 != null){
                                            app.ajax({
                                                xurl:'crm/gwei005/getInfo',
                                                xdata:{
                                                    GWEC005:vueData.form.GWEC005,
                                                    GWECPLANT:vueData.form.GWECPLANT
                                                },
                                                type:'POST',
                                                dataType:'json',
                                                async:false,
                                                cb: function (result) {
                                                    vueData.form.GWEC106 = result.data.data.GWEC106;
                                                    vueData.form.GWEC107 = result.data.data.GWEC107;
                                                    vueData.form.GWEC108 = result.data.data.GWEC108;
                                                }
                                            })
                                        }
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择归属法人',
                                    area: ['600px', '430px']
                                }
                            })
                        }
                    },

                    //添加按钮事件
                    add:function(){
                        if(vueData.form.GWEC005 == '' || vueData.form.GWEC005 == undefined){
                            app.showMessage('请先选择客户！');
                            return;
                        }
                        // if(vueData.form.GWECMLEGAL == '' || vueData.form.GWECMLEGAL == undefined ){
                        //     app.showMessage('请先选择归属法人！');
                        //     return;
                        // }
                        var list = gwecTable.getDatas();
                        var data = [];
                        for (var i = 0; i <= list.length - 1; i++) {
                            if (list[i]["GWEC003"] != '') {
                                var str = list[i]["GWEC003"] + '-' + list[i]["GWEC101"];
                                data.push(str);
                            }
                        }
                        self._openTplFull({
                            tag: 'gwei005_edit_add',
                            tagData: {
                                OEB:data.join(','),
                                GWEC005:vueData.form.GWEC005,
                                GWAKL004:vueData.form.GWAKL004,
                                GWECDOCNO:vueData.form.GWECDOCNO
                                // GWECPLANT:vueData.form.GWECPLANT,
                                // GWECDOCNO:vueData.form.GWECDOCNO
                            },
                            index: 0,
                            openOpts: {
                                title: '选择销售订单',
                                area: ['900px', '450px']
                            }
                        })
                    },

                    delRows:function(){
                        var tableList=gwecTable.getDatas();
                        var checkStatus = table.checkStatus("gwei005_edit_table");
                        if(checkStatus.data.length>0){
                            checkStatus.data.forEach(function (value) {
                                tableList.removeByMap({
                                    GWECSEQ:value.GWECSEQ
                                })
                            });
                            var count=1;
                            tableList.forEach(function (value) {
                                value.ROW_ID=count++;
                            })
                            listData=tableList;
                            self.tableReload(listData);
                        }
                    },
                    clearMlegal:function () {
                        if(vueData.form.NAME_AZP != '' || vueData.form.NAME_AZP != null){
                            vueData.form.GWECPLANT = '';
                            vueData.form.AZP02 = '';
                        }
                    }
                },

                watch: {
                    'form.GWEC002': function () {

                        var length=gwecTable.getDatas().length;
                        if(length!=0){
                            listData=gwecTable.getDatas();
                            listData.forEach(function (value) {
                                value.GWEC002=vueData.form.GWEC002;
                            });
                            self.tableReload(listData);
                        }
                    },
                    'form.GWEC011': function () {

                        var length=gwecTable.getDatas().length;
                        if(length!=0){
                            listData=gwecTable.getDatas();
                            listData.forEach(function (value) {
                                value.GWEC011=vueData.form.GWEC011;
                                value.NAME_USER=vueData.form.NAME_USER;
                            });
                            self.tableReload(listData);
                        }

                    },
                    'form.GWEC012': function () {
                        var length=gwecTable.getDatas().length;
                        if(length!=0){
                            listData=gwecTable.getDatas();
                            listData.forEach(function (value) {
                                value.GWEC012=vueData.form.GWEC012;
                                value.NAME_ORG=vueData.form.NAME_ORG;
                            });
                            self.tableReload(listData);
                        }

                    }
                },
                created: function () {
                    self._setEleIsReady('gwei005_edit');
                }
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwei005Edit();

});
